package com.acme.faturamento.view.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.acme.faturamento.model.entity.Cliente;
import com.acme.faturamento.view.businessdelegate.PersistenceManagerBD;

public class ClienteAction extends DispatchAction {

    /**
     * Novo
     */
    public ActionForward novo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        setVo(form,new Cliente());
        return mapping.findForward("edicao");
    }

    /**
     * Pesquisar
     */
    public ActionForward pesquisar(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        PersistenceManagerBD persiste = new PersistenceManagerBD();

        request.setAttribute("clienteLista",persiste.<Cliente>getLista("findClientes"));
        return mapping.findForward("pesquisa");
    }

    /**
     * Salvar
     *
     */
    public ActionForward salvar(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        Cliente cliente = getVo(form);

        System.out.println("CLIENTE :[" + cliente.toString() + "]");
        PersistenceManagerBD persiste = new PersistenceManagerBD();
        persiste.mesclar(cliente);

        ActionMessages messages = new ActionMessages();
        messages.add("msg", new ActionMessage("global.detail", "Salvo com sucesso !"));
        saveMessages(request, messages);

        request.setAttribute("metodo", "***salvar***");
        return mapping.findForward("sucesso");
    }

    /**
     * Alterar
     *
     */
    public ActionForward alterar(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {


        Cliente cliente = getVo(form);

        System.out.println("ID : "+request.getParameter("id"));

        PersistenceManagerBD persiste = new PersistenceManagerBD();
        cliente = persiste.get(Cliente.class,Long.valueOf(request.getParameter("id")));
        System.out.println("Cliente:"+cliente.toString());
        setVo(form,cliente);

        return mapping.findForward("edicao");
    }

    /**
     * Apagar
     *
     */
    public ActionForward apagar(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        PersistenceManagerBD persiste = new PersistenceManagerBD();
        persiste.apagar(Cliente.class,getVo(form).getId());

        ActionMessages messages = new ActionMessages();
        messages.add("msg", new ActionMessage("global.detail", "Registro apagado !"));
        saveMessages(request, messages);

        return mapping.findForward("pesquisa");
    }

    public ActionForward preparaPesquisa(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        return mapping.findForward("pesquisa");
    }

    private Cliente getVo(ActionForm form){
        DynaActionForm formulario = (DynaActionForm) form;
        Cliente cliente = (Cliente) formulario.get("vo");
        return cliente;
    }
    private void setVo(ActionForm form,Cliente vo){
        DynaActionForm formulario = (DynaActionForm) form;
        formulario.set("vo",vo);

    }
}
